
#ifndef   __OPERATION_SYSTEM_H_
#define   __OPERATION_SYSTEM_H_

#define   MS_NOT_ACTIVE		0
#define   MS_ACTIVE			1
#define   MS_FINISH			2
#define   MS_SLEEP			3

#define   APP_SIGNAL_CLOSE	1

#define   MODULE_STOP		(-1)	  /* for each module must be `case START:` START == 1 */
#define   MODULE_RESET		(-2)	  /* for each module must be `case START:` START == 1 */

#define   APP_AMOUNT					10		/* Max. Amount of MODULEs into main loop */

#define   APP_STACK_LEVEL_MAX		5		

#define   OS_APP_RESULT_VOID		0
#define   OS_APP_RESULT				1

typedef void exe(void) ;
typedef struct app_def
{
		void (*exe)(void);
		unsigned short State;	  /* for state mashine */
		unsigned short Signal;	  /* send signal to app */
		unsigned short Result;		/* App result */
		unsigned short CallResult;	/* result after call of OS_Call_App(); */
		unsigned short Status;	  /* APP_STATUS_SLEEP */
		unsigned short TimerCase; /* case of switch */
		unsigned int 	SleepTimer;
		unsigned int	Timer;			

}app_def_t;


typedef struct app_t
{
		app_def_t App[APP_STACK_LEVEL_MAX];
		int StackLevel;
}app_t;


extern app_t Apps[];
extern int OS_CurrentAppIndex;

#define   APP_STACK_LEVEL	Apps[OS_CurrentAppIndex].StackLevel
#define   APP	Apps[OS_CurrentAppIndex].App[APP_STACK_LEVEL]


#define   APP_NOT_FOUND				(-1)
#define   NO_FREE_MEM				(-1)

/* OS errors */
enum
{
	OS_ERR_APP_NOT_FOUND = -1,
	OS_ERR_NO_FREE_INDEX = -2,
	OS_ERR_APP_ALREADY_STARTED = -3
};

#define   OS_SET_TIMER(t_ms, CASE) 	APP.Timer=((t_ms/T0_CLK)+1);APP.TimerCase=CASE 



void OS_Reset ( void );

void OS_Init ( void );
void OS_Sleep(int SleepTime );
void OS_Apps ( void );
int OS_Find_App ( exe *App );
int OS_Run_App ( exe *App );
//int OS_Call_App( exe *App );
int OS_Call_App( int call_param, exe *App );
void OS_Return( void );
void OS_App_Timers ( void );
int OS_GetAppResult ( int AppIndex );
int OS_Send_Signal ( exe *App, unsigned short signal );
int OS_Kill_App ( exe *App );

#endif
